home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_022 / lemacs / file.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  16KB  |  504 lines

  1. /*
  2.  * The routines in this file
  3.  * handle the reading and writing of
  4.  * disk files. All of details about the
  5.  * reading and writing of the disk are
  6.  * in "fileio.c".
  7.  */
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11.  
  12. /*
  13.  * Read a file into the current
  14.  * buffer. This is really easy; all you do it
  15.  * find the name of the file, and call the standard
  16.  * "read a file into the current buffer" code.
  17.  * Bound to "C-X C-R".
  18.  */
  19. fileread(f, n)
  20. {
  21.         register int    s;
  22.         char fname[NFILEN];
  23.  
  24.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  25.                 return(s);
  26.         return(readin(fname, TRUE));
  27. }
  28.  
  29. /*
  30.  * Insert a file into the current
  31.  * buffer. This is really easy; all you do it
  32.  * find the name of the file, and call the standard
  33.  * "insert a file into the current buffer" code.
  34.  * Bound to "C-X C-I".
  35.  */
  36. insfile(f, n)
  37. {
  38.         register int    s;
  39.         char fname[NFILEN];
  40.  
  41.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  42.         return(rdonly());    /* we are in read only mode    */
  43.         if ((s=mlreply("Insert file: ", fname, NFILEN)) != TRUE)
  44.                 return(s);
  45.         return(ifile(fname));
  46. }
  47.  
  48. /*
  49.  * Select a file for editing.
  50.  * Look around to see if you can find the
  51.  * fine in another buffer; if you can find it
  52.  * just switch to the buffer. If you cannot find
  53.  * the file, create a new buffer, read in the
  54.  * text, and switch to the new buffer.
  55.  * Bound to C-X C-F.
  56.  */
  57. filefind(f, n)
  58. {
  59.         char fname[NFILEN];    /* file user wishes to find */
  60.         register int s;        /* status return */
  61.  
  62.         if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE)
  63.                 return(s);
  64.     return(getfile(fname, TRUE));
  65. }
  66.  
  67. viewfile(f, n)    /* visit a file in VIEW mode */
  68. {
  69.         char fname[NFILEN];    /* file user wishes to find */
  70.         register int s;        /* status return */
  71.     register WINDOW *wp;    /* scan for windows that need updating */
  72.  
  73.         if ((s=mlreply("View file: ", fname, NFILEN)) != TRUE)
  74.                 return (s);
  75.     s = getfile(fname, FALSE);
  76.     if (s) {    /* if we succeed, put it in view mode */
  77.         curwp->w_bufp->b_mode |= MDVIEW;
  78.  
  79.         /* scan through and update mode lines of all windows */
  80.         wp = wheadp;
  81.         while (wp != NULL) {
  82.             wp->w_flag |= WFMODE;
  83.             wp = wp->w_wndp;
  84.         }
  85.     }
  86.     return(s);
  87. }
  88.  
  89. getfile(fname, lockfl)
  90.  
  91. char fname[];        /* file name to find */
  92. int lockfl;        /* check the file for locks? */
  93.  
  94. {
  95.         register BUFFER *bp;
  96.         register LINE   *lp;
  97.         register int    i;
  98.         register int    s;
  99.         char bname[NBUFN];    /* buffer name to put file */
  100.  
  101.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  102.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  103.                         if (--curbp->b_nwnd == 0) {
  104.                                 curbp->b_dotp  = curwp->w_dotp;
  105.                                 curbp->b_doto  = curwp->w_doto;
  106.                                 curbp->b_markp = curwp->w_markp;
  107.                                 curbp->b_marko = curwp->w_marko;
  108.                         }
  109.             swbuffer(bp);
  110.                         lp = curwp->w_dotp;
  111.                         i = curwp->w_ntrows/2;
  112.                         while (i-- && lback(lp)!=curbp->b_linep)
  113.                                 lp = lback(lp);
  114.                         curwp->w_linep = lp;
  115.                         curwp->w_flag |= WFMODE|WFHARD;
  116.                         mlwrite("[Old buffer]");
  117.                         return (TRUE);
  118.                 }
  119.         }
  120.         makename(bname, fname);                 /* New buffer name.     */
  121.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  122.                 s = mlreply("Buffer name: ", bname, NBUFN);
  123.                 if (s == ABORT)                 /* ^G to just quit      */
  124.                         return (s);
  125.                 if (s == FALSE) {               /* CR to clobber it     */
  126.                         makename(bname, fname);
  127.                         break;
  128.                 }
  129.         }
  130.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  131.                 mlwrite("Cannot create buffer");
  132.                 return (FALSE);
  133.         }
  134.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  135.                 curbp->b_dotp = curwp->w_dotp;
  136.                 curbp->b_doto = curwp->w_doto;
  137.                 curbp->b_markp = curwp->w_markp;
  138.                 curbp->b_marko = curwp->w_marko;
  139.         }
  140.         curbp = bp;                             /* Switch to it.        */
  141.         curwp->w_bufp = bp;
  142.         curbp->b_nwnd++;
  143.         return(readin(fname, lockfl));          /* Read it in.          */
  144. }
  145.  
  146. /*
  147.  * Read file "fname" into the current
  148.  * buffer, blowing away any text found there. Called
  149.  * by both the read and find commands. Return the final
  150.  * status of the read. Also called by the mainline,
  151.  * to read in a file specified on the command line as
  152.  * an argument. If the filename ends in a ".c", CMODE is
  153.  * set for the current buffer.
  154.  */
  155. readin(fname, lockfl)
  156.  
  157. char    fname[];    /* name of file to read */
  158. int    lockfl;        /* check for file locks? */
  159.  
  160. {
  161.         register LINE   *lp1;
  162.         register LINE   *lp2;
  163.         register int    i;
  164.         register WINDOW *wp;
  165.         register BUFFER *bp;
  166.         register int    s;
  167.         register int    nbytes;
  168.         register int    nline;
  169.     register char    *sptr;        /* pointer into filename string */
  170.     int        lflag;        /* any lines longer than allowed? */
  171.         char            line[NLINE];
  172.  
  173. #if    FILOCK
  174.     if (lockfl && lockchk(fname) == ABORT)
  175.         return(ABORT);
  176. #endif
  177.         bp = curbp;                             /* Cheap.               */
  178.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  179.                 return (s);
  180.         bp->b_flag &= ~(BFTEMP|BFCHG);
  181.     if (strlen(fname) > 1) {        /* check if a 'C' file    */
  182.         sptr = fname + strlen(fname) - 2;
  183.         if (*sptr == '.' &&
  184.            *(sptr + 1) == 'c' || *(sptr + 1) == 'h')
  185.             bp->b_mode |= MDCMOD;
  186.     }
  187.         strcpy(bp->b_fname, fname);
  188.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  189.                 goto out;
  190.         if (s == FIOFNF) {                      /* File not found.      */
  191.                 mlwrite("[New file]");
  192.                 goto out;
  193.         }
  194.         mlwrite("[Reading file]");
  195.         nline = 0;
  196.     lflag = FALSE;
  197.         while ((s=ffgetline(line, NLINE)) == FIOSUC || s == FIOLNG) {
  198.         if (s == FIOLNG)
  199.             lflag = TRUE;
  200.                 nbytes = strlen(line);
  201.                 if ((lp1=lalloc(nbytes)) == NULL) {
  202.                         s = FIOERR;             /* Keep message on the  */
  203.                         break;                  /* display.             */
  204.                 }
  205.                 lp2 = lback(curbp->b_linep);
  206.                 lp2->l_fp = lp1;
  207.                 lp1->l_fp = curbp->b_linep;
  208.                 lp1->l_bp = lp2;
  209.                 curbp->b_linep->l_bp = lp1;
  210.                 for (i=0; i<nbytes; ++i)
  211.                         lputc(lp1, i, line[i]);
  212.                 ++nline;
  213.         }
  214.         ffclose();                              /* Ignore errors.       */
  215.         if (s == FIOEOF) {                      /* Don't zap message!   */
  216.                 if (nline == 1)
  217.                         mlwrite("[Read 1 line]");
  218.                 else
  219.                         mlwrite("[Read %d lines]", nline);
  220.         }
  221.     if (lflag)
  222.         mlwrite("[Read %d line(s), Long lines wrapped]",nline);
  223. out:
  224.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  225.                 if (wp->w_bufp == curbp) {
  226.                         wp->w_linep = lforw(curbp->b_linep);
  227.                         wp->w_dotp  = lforw(curbp->b_linep);
  228.                         wp->w_doto  = 0;
  229.                         wp->w_markp = NULL;
  230.                         wp->w_marko = 0;
  231.                         wp->w_flag |= WFMODE|WFHARD;
  232.                 }
  233.         }
  234.         if (s == FIOERR || s == FIOFNF)        /* False if error.      */
  235.                 return(FALSE);
  236.         return (TRUE);
  237. }
  238.  
  239. /*
  240.  * Take a file name, and from it
  241.  * fabricate a buffer name. This routine knows
  242.  * about the syntax of file names on the target system.
  243.  * I suppose that this information could be put in
  244.  * a better place than a line of code.
  245.  */
  246. makename(bname, fname)
  247. char    bname[];
  248. char    fname[];
  249. {
  250.         register char   *cp1;
  251.         register char   *cp2;
  252.  
  253.         cp1 = &fname[0];
  254.         while (*cp1 != 0)
  255.                 ++cp1;
  256.  
  257. #if     AMIGA
  258.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  259.                 --cp1;
  260. #endif
  261. #if     VMS
  262.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  263.                 --cp1;
  264. #endif
  265. #if     CPM
  266.         while (cp1!=&fname[0] && cp1[-1]!=':')
  267.                 --cp1;
  268. #endif
  269. #if     MSDOS
  270.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  271.                 --cp1;
  272. #endif
  273. #if     V7
  274.         while (cp1!=&fname[0] && cp1[-1]!='/')
  275.                 --cp1;
  276. #endif
  277.         cp2 = &bname[0];
  278.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  279.                 *cp2++ = *cp1++;
  280.         *cp2 = 0;
  281. }
  282.  
  283. /*
  284.  * Ask for a file name, and write the
  285.  * contents of the current buffer to that file.
  286.  * Update the remembered file name and clear the
  287.  * buffer changed flag. This handling of file names
  288.  * is different from the earlier versions, and
  289.  * is more compatable with Gosling EMACS than
  290.  * with ITS EMACS. Bound to "C-X C-W".
  291.  */
  292. filewrite(f, n)
  293. {
  294.         register WINDOW *wp;
  295.         register int    s;
  296.         char            fname[NFILEN];
  297.  
  298.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  299.                 return (s);
  300.         if ((s=writeout(fname)) == TRUE) {
  301.                 strcpy(curbp->b_fname, fname);
  302.                 curbp->b_flag &= ~BFCHG;
  303.                 wp = wheadp;                    /* Update mode lines.   */
  304.                 while (wp != NULL) {
  305.                         if (wp->w_bufp == curbp)
  306.                                 wp->w_flag |= WFMODE;
  307.                         wp = wp->w_wndp;
  308.                 }
  309.         }
  310.         return (s);
  311. }
  312.  
  313. /*
  314.  * Save the contents of the current
  315.  * buffer in its associatd file. No nothing
  316.  * if nothing has changed (this may be a bug, not a
  317.  * feature). Error if there is no remembered file
  318.  * name for the buffer. Bound to "C-X C-S". May
  319.  * get called by "C-Z".
  320.  */
  321. filesave(f, n)
  322. {
  323.         register WINDOW *wp;
  324.         register int    s;
  325.  
  326.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  327.         return(rdonly());    /* we are in read only mode    */
  328.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  329.                 return (TRUE);
  330.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  331.                 mlwrite("No file name");
  332.                 return (FALSE);
  333.         }
  334.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  335.                 curbp->b_flag &= ~BFCHG;
  336.                 wp = wheadp;                    /* Update mode lines.   */
  337.                 while (wp != NULL) {
  338.                         if (wp->w_bufp == curbp)
  339.                                 wp->w_flag |= WFMODE;
  340.                         wp = wp->w_wndp;
  341.                 }
  342.         }
  343.         return (s);
  344. }
  345.  
  346. /*
  347.  * This function performs the details of file
  348.  * writing. Uses the file management routines in the
  349.  * "fileio.c" package. The number of lines written is
  350.  * displayed. Sadly, it looks inside a LINE; provide
  351.  * a macro for this. Most of the grief is error
  352.  * checking of some sort.
  353.  */
  354. writeout(fn)
  355. char    *fn;
  356. {
  357.         register int    s;
  358.         register LINE   *lp;
  359.         register int    nline;
  360.  
  361.         if ((s=ffwopen(fn)) != FIOSUC)          /* Open writes message. */
  362.                 return (FALSE);
  363.     mlwrite("[Writing..]");            /* tell us were writing */
  364.         lp = lforw(curbp->b_linep);             /* First line.          */
  365.         nline = 0;                              /* Number of lines.     */
  366.         while (lp != curbp->b_linep) {
  367.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  368.                         break;
  369.                 ++nline;
  370.                 lp = lforw(lp);
  371.         }
  372.         if (s == FIOSUC) {                      /* No write error.      */
  373.                 s = ffclose();
  374.                 if (s == FIOSUC) {              /* No close error.      */
  375.                         if (nline == 1)
  376.                                 mlwrite("[Wrote 1 line]");
  377.                         else
  378.                                 mlwrite("[Wrote %d lines]", nline);
  379.                 }
  380.         } else                                  /* Ignore close error   */
  381.                 ffclose();                      /* if a write error.    */
  382.         if (s != FIOSUC)                        /* Some sort of error.  */
  383.                 return (FALSE);
  384.         return (TRUE);
  385. }
  386.  
  387. /*
  388.  * The command allows the user
  389.  * to modify the file name associated with
  390.  * the current buffer. It is like the "f" command
  391.  * in UNIX "ed". The operation is simple; just zap
  392.  * the name in the BUFFER structure, and mark the windows
  393.  * as needing an update. You can type a blank line at the
  394.  * prompt if you wish.
  395.  */
  396. filename(f, n)
  397. {
  398.         register WINDOW *wp;
  399.         register int    s;
  400.         char            fname[NFILEN];
  401.  
  402.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  403.                 return (s);
  404.         if (s == FALSE)
  405.                 strcpy(curbp->b_fname, "");
  406.         else
  407.                 strcpy(curbp->b_fname, fname);
  408.         wp = wheadp;                            /* Update mode lines.   */
  409.         while (wp != NULL) {
  410.                 if (wp->w_bufp == curbp)
  411.                         wp->w_flag |= WFMODE;
  412.                 wp = wp->w_wndp;
  413.         }
  414.     curbp->b_mode &= ~MDVIEW;    /* no longer read only mode */
  415.         return (TRUE);
  416. }
  417.  
  418. /*
  419.  * Insert file "fname" into the current
  420.  * buffer, Called by insert file command. Return the final
  421.  * status of the read.
  422.  */
  423. ifile(fname)
  424. char    fname[];
  425. {
  426.         register LINE   *lp0;
  427.         register LINE   *lp1;
  428.         register LINE   *lp2;
  429.         register int    i;
  430.         register BUFFER *bp;
  431.         register int    s;
  432.         register int    nbytes;
  433.         register int    nline;
  434.     int        lflag;        /* any lines longer than allowed? */
  435.         char            line[NLINE];
  436.  
  437.         bp = curbp;                             /* Cheap.               */
  438.         bp->b_flag |= BFCHG;            /* we have changed    */
  439.     bp->b_flag &= ~BFTEMP;            /* and are not temporary*/
  440.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  441.                 goto out;
  442.         if (s == FIOFNF) {                      /* File not found.      */
  443.                 mlwrite("[No such file]");
  444.         return(FALSE);
  445.         }
  446.         mlwrite("[Inserting file]");
  447.  
  448.     /* back up a line and save the mark here */
  449.     curwp->w_dotp = lback(curwp->w_dotp);
  450.     curwp->w_doto = 0;
  451.     curwp->w_markp = curwp->w_dotp;
  452.     curwp->w_marko = 0;
  453.  
  454.         nline = 0;
  455.     lflag = FALSE;
  456.         while ((s=ffgetline(line, NLINE)) == FIOSUC || s == FIOLNG) {
  457.         if (s == FIOLNG)
  458.             lflag = TRUE;
  459.                 nbytes = strlen(line);
  460.                 if ((lp1=lalloc(nbytes)) == NULL) {
  461.                         s = FIOERR;             /* Keep message on the  */
  462.                         break;                  /* display.             */
  463.                 }
  464.         lp0 = curwp->w_dotp;    /* line previous to insert */
  465.         lp2 = lp0->l_fp;    /* line after insert */
  466.  
  467.         /* re-link new line between lp0 and lp2 */
  468.         lp2->l_bp = lp1;
  469.         lp0->l_fp = lp1;
  470.         lp1->l_bp = lp0;
  471.         lp1->l_fp = lp2;
  472.  
  473.         /* and advance and write out the current line */
  474.         curwp->w_dotp = lp1;
  475.                 for (i=0; i<nbytes; ++i)
  476.                         lputc(lp1, i, line[i]);
  477.                 ++nline;
  478.         }
  479.         ffclose();                              /* Ignore errors.       */
  480.     curwp->w_markp = lforw(curwp->w_markp);
  481.         if (s == FIOEOF) {                      /* Don't zap message!   */
  482.                 if (nline == 1)
  483.                         mlwrite("[Inserted 1 line]");
  484.                 else
  485.                         mlwrite("[Inserted %d lines]", nline);
  486.         }
  487.     if (lflag)
  488.         mlwrite("[Inserted %d line(s), Long lines wrapped]",nline);
  489. out:
  490.     /* advance to the next line and mark the window for changes */
  491.     curwp->w_dotp = lforw(curwp->w_dotp);
  492.     curwp->w_flag |= WFHARD;
  493.  
  494.     /* copy window parameters back to the buffer structure */
  495.     curbp->b_dotp = curwp->w_dotp;
  496.     curbp->b_doto = curwp->w_doto;
  497.     curbp->b_markp = curwp->w_markp;
  498.     curbp->b_marko = curwp->w_marko;
  499.  
  500.         if (s == FIOERR)                        /* False if error.      */
  501.                 return (FALSE);
  502.         return (TRUE);
  503. }
  504.